package com.wang.web3.utils;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

/**
 * @author Mr.Wang
 * @version 1.0
 * @time 2017/5/2
 * @describe
 */

public class FragmentUtils {

    public static Fragment preFragment;

    public static Fragment getPreFragment() {
        return preFragment;
    }

    public static void setCurrentFragment(Fragment currentFragment) {
        preFragment = currentFragment;
    }

    public static void showActivity(FragmentManager fragmentManager, int containerID, Fragment showFragment) {
        FragmentTransaction transition = fragmentManager.beginTransaction();
        transition.add(containerID, showFragment);
        transition.commit();
    }


    public static void show(FragmentManager fragmentManager, int containerID, Fragment showFragment) {
        if (preFragment == null)
            show(fragmentManager, containerID, showFragment, null);
        else
            show(fragmentManager, containerID, showFragment, preFragment);
    }

    public static void show(FragmentManager fragmentManager, int containerID, Fragment showFragment, Fragment hideFragment) {
        FragmentTransaction transition = fragmentManager.beginTransaction();
        if (!showFragment.isAdded()) {
            transition.add(containerID, showFragment);
//            transition.add(containerID,showFragment).addToBackStack(null);
        }
        if (hideFragment == null)
            transition.show(showFragment);
        else
            transition.hide(preFragment).show(showFragment);  // 这里先隐藏后显示  是为了解决 后退时选中的是第一个 再进程序时 第一个被隐藏的Bug
        transition.commit();
        preFragment = showFragment;
    }
}
